/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-07-09
 * Time: 18:50
 */
public class MyLinkedList implements IMyLinkedList{
    ListNode head;
    ListNode tail;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        if (head == null) {
            head = tail = node;
            return;
        }

        node.next = head;
        head.prev = node;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if (head == null) {
            head = tail = node;
            return;
        }
        tail.next = node;
        node.prev = tail;
        tail = node;
    }

    @Override
    public boolean addIndex(int index, int data) {
        int size = size();
        if (index < 0 || index > size) {
            return false;
        }

        if (index == 0) {
            addFirst(data);
            return true;
        }
        if (index == size) {
            addLast(data);
            return true;
        }

        ListNode node = new ListNode(data);
        ListNode cur = head;
        int count = index;
        while (count != 0) {
            cur = cur.next;
            count--;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;

        return true;
    }

    @Override
    public boolean contains(int key) {
        if (head == null) {
            return false;
        }

        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head == null) {
            return;
        }

        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur.next != null && cur.prev != null) {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                    return;
                }
                if (cur.next == null && cur.prev == null) {
                    head = tail = null;
                    return;
                }
                if (cur.next == null) {
                    tail = tail.prev;
                    tail.next = null;
                }
                if (cur.prev == null) {
                    head = head.next;
                    head.prev = null;
                }
            }
            cur = cur.next;
        }

    }

    @Override
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }

        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur.next != null && cur.prev != null) {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                if (cur.next == null && cur.prev == null) {
                    head = tail = null;
                    return;
                }
                if (cur.next == null) {
                    tail = tail.prev;
                    tail.next = null;
                }
                if (cur.prev == null) {
                    head = head.next;
                    head.prev = null;
                }
            }
            cur = cur.next;
        }


    }

    @Override
    public int size() {
        int size = 0;
        ListNode cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    @Override
    public void display() {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    /*@Override
    public void clear() {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
    }*/

    @Override
    public void clear() {
        ListNode cur = head;

        while (cur != null) {
            ListNode curN = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
        head = null;
        tail = null;
    }


    static class ListNode {
        ListNode next;
        ListNode prev;
        int val;

        public ListNode(int val) {
            this.val = val;
        }
    }
}
